package com.intellagric.service.impl.module.sys_dictionary;

import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.mapper.SysDictionaryMapper;
import com.intellagric.pojo.SysDictionary;
import com.intellagric.pojo.SysDictionaryExample;
import com.intellagric.pojo.SysMenu;
import com.intellagric.service.module.sys_dictionary.DictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2018/10/30
 * @DESC： 数据字典管理服务实现
 */
@Service
public class DictionaryServiceImpl implements DictionaryService {

    @Autowired
    private SysDictionaryMapper dictionaryMapper;

    /**
     * 添加字典信息
     * @param sysDictionary
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage addDictionary(SysDictionary sysDictionary) {
        if (dictionaryMapper.insertSelective(sysDictionary) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 删除字典信息
     * @param id
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage deleteDictionary(String id) {
        if (dictionaryMapper.deleteByPrimaryKey(id) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 修改字典信息
     * @param sysDictionary
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage editDictionary(SysDictionary sysDictionary) {
        if (dictionaryMapper.updateByPrimaryKeySelective(sysDictionary) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 通过id查询字典信息
     * @param id
     * @return SysDictionary
     */
    @Override
    public SysDictionary getDictionaryById(String id) {
        return dictionaryMapper.selectByPrimaryKey(id);
    }

    /**
     * 通过parentId查询字典信息
     * @param parentId
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getDictionaryByParentId(String parentId) {
        SysDictionaryExample dictionaryExample = new SysDictionaryExample();
        dictionaryExample.createCriteria().andParentIdEqualTo(parentId);
        List<SysDictionary> dictionaryList = dictionaryMapper.selectByExample(dictionaryExample);
        return LayuiDataGridResult.success().add(dictionaryList,dictionaryList.size());
    }

    /**
     * 查询字典信息列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getDictionaryList() {
        SysDictionaryExample dictionaryExample = new SysDictionaryExample();
        dictionaryExample.createCriteria().andIdIsNotNull();
        List<SysDictionary> dictionaryList = dictionaryMapper.selectByExample(dictionaryExample);
        return LayuiDataGridResult.success().add(dictionaryList,dictionaryList.size());
    }

    /**
     * 查询字典列表，并生成树形数据
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getDictionaryTree() {
        //查询字典信息列表
        List<SysDictionary> dictionaryList = this.getDictionaryList().getData();

        //查询统计字典列表信息
        List<SysDictionary> rootDictionaryList = this.getDictionaryByParentId("0").getData();

        for (SysDictionary dictionary : rootDictionaryList) {
            dictionary.setChildren(getChild(dictionary.getId(), dictionaryList));
        }
        return LayuiDataGridResult.success().add(rootDictionaryList,rootDictionaryList.size());
    }

    private List<SysDictionary> getChild(String id, List<SysDictionary> dictionaryList) {
        // 子菜单
        List<SysDictionary> childList = new ArrayList<>();

        // 遍历所有节点，将父菜单id与传过来的id比较
        for (SysDictionary dictionary : dictionaryList) {
            if (dictionary.getParentId() != null) {   //所有非以及菜单的都要加入chidList中（递归）
                if (dictionary.getParentId().equals(id)) {
                    childList.add(dictionary);
                }
            }
        }

        // 把子菜单的子菜单再循环一遍
        for (SysDictionary dictionary : childList) {// 没有url子菜单还有子菜单（暂不考虑url的问题）
            // 递归
            dictionary.setChildren(getChild(dictionary.getId(), dictionaryList));
        } // 递归退出条件
        if (childList.size() == 0) {
            return null;
        }
        return childList;
    }


}
